package visao;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import logica.CalculoProposicional;
import logica.CalculoProposicional.Formula;

public class JanelaPrincipal extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JTextField txtFormula;
	private JPanel panel_valores;
	private JButton btnTrocar;
	private JButton btnValorarFrmula;
	private JButton btnTodosParaFalso;
	private JButton btnTodosParaVerdade;
	private JButton btnLerDeUm;
	private JButton btnSalvar;
	private JLabel lblStatus;
	private JProgressBar progressBar;
	
	public JanelaPrincipal()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Solvedor Lógico Caqui");
		setSize(new Dimension(640, 480));
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
		rigidArea.setPreferredSize(new Dimension(20, 5));
		panel.add(rigidArea);
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();
		flowLayout.setAlignment(FlowLayout.LEADING);
		panel.add(panel_4);
		
		JLabel lblFrmula = new JLabel("Fórmula:");
		panel_4.add(lblFrmula);
		
		Component rigidArea_3 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_3.setPreferredSize(new Dimension(12, 5));
		panel.add(rigidArea_3);
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		
		Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_1.setPreferredSize(new Dimension(5, 20));
		panel_1.add(rigidArea_1);
		
		txtFormula = new JTextField();
		txtFormula.setEditable(false);
		panel_1.add(txtFormula);
		txtFormula.setMargin(new Insets(8, 8, 8, 8));
		txtFormula.setColumns(10);
		
		Component rigidArea_5 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_5.setPreferredSize(new Dimension(5, 20));
		panel_1.add(rigidArea_5);
		
		btnTrocar = new JButton("Trocar");
		btnTrocar.addActionListener(this);
		btnTrocar.setPreferredSize(new Dimension(96, 28));
		panel_1.add(btnTrocar);
		
		Component rigidArea_2 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_2.setPreferredSize(new Dimension(5, 20));
		panel_1.add(rigidArea_2);
		
		Component rigidArea_8 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_8.setPreferredSize(new Dimension(20, 5));
		panel.add(rigidArea_8);
		
		JPanel panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.X_AXIS));
		
		Component rigidArea_4 = Box.createRigidArea(new Dimension(20, 20));
		panel_2.add(rigidArea_4);
		
		progressBar = new JProgressBar();
		progressBar.setPreferredSize(new Dimension(64, 14));
		progressBar.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel_2.add(progressBar);
		
		JPanel panel_5 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_5.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_5);
		
		lblStatus = new JLabel("Parado");
		panel_5.add(lblStatus);
		
		JPanel panel_3 = new JPanel();
		getContentPane().add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_3.add(scrollPane);
		
		panel_valores = new JPanel();
		scrollPane.setViewportView(panel_valores);
		
		JPanel panel_6 = new JPanel();
		panel_3.add(panel_6, BorderLayout.SOUTH);
		
		btnValorarFrmula = new JButton("Valorar fórmula");
		panel_6.add(btnValorarFrmula);
		
		btnTodosParaFalso = new JButton("Todos para falso");
		panel_6.add(btnTodosParaFalso);
		
		btnTodosParaVerdade = new JButton("Todos para verdade");
		panel_6.add(btnTodosParaVerdade);
		
		btnLerDeUm = new JButton("Ler de um arquivo");
		panel_6.add(btnLerDeUm);
		
		btnSalvar = new JButton("Salvar em arquivo");
		panel_6.add(btnSalvar);
		
		Component rigidArea_6 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_6.setPreferredSize(new Dimension(5, 20));
		getContentPane().add(rigidArea_6, BorderLayout.WEST);
		
		Component rigidArea_7 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_7.setPreferredSize(new Dimension(5, 20));
		getContentPane().add(rigidArea_7, BorderLayout.EAST);
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		JanelaPrincipal j = new JanelaPrincipal();
		j.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		Object source = arg0.getSource();
		
		if(source == null)
			return;
		
		else if(source == btnTrocar)
		{
			String expressao;
			Formula f;
			expressao = JOptionPane.showInputDialog(this, "Entre com a formula nova", "Trocar formula", JOptionPane.QUESTION_MESSAGE);
			if(expressao != null)
			{
				if(expressao.trim().equals(""))
				{
					JOptionPane.showMessageDialog(this, "Campo vazio! Digite uma formula", "Campo vazio", JOptionPane.WARNING_MESSAGE);
				}
				try
				{
					lblStatus.setText("Interpretando...");
					f = CalculoProposicional.PADRAO.new Formula(expressao.trim());
					lblStatus.setText("Parado");
//					System.out.println(f.toString());
					txtFormula.setText(expressao.trim());
					JOptionPane.showMessageDialog(this,"O interpretador reconheceu a expressao \""+expressao+"\" com sucesso!", "Sucesso!", JOptionPane.INFORMATION_MESSAGE);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(this, e.getMessage()+"\n Tente novamente!", "Erro!", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		
	}

}
